import { config } from 'utils'
import React from 'react'
import PropTypes from 'prop-types'
import { routerRedux } from 'dva/router'
import { connect } from 'dva'
import { Row, Col, Button, Popconfirm } from 'antd'
import queryString from 'query-string'
import { Page, List } from 'components'
import { ListMenuType } from 'utils/enums'
import Filter from './Filter'
import {transactionStatus} from '../../utils/seletLocalData'



const transactionReport = ({ location, dispatch,  transactionReport, loading }) => {
  location.query = queryString.parse(location.search)
  const { list, pagination, currentItem, modalVisible, editType,  selectedRowKeys } = transactionReport
  const { pageSize } = pagination
  const listProps = {
    dataSource: list,
    menuType: ListMenuType.MENU_UPDATE + ListMenuType.MENU_REPORT,
    loading: loading.effects['transactionReport/query'],
    columnsData: [
      {
        title: 'ID',
        dataIndex: 'id',
        key: 'id',
      },
      {
        title: 'UpdateTime',
        dataIndex: 'updateTime',
        key: 'updateTime',
      },
      {
        title: 'CUSTOMER_NAME',
        key: 'CUSTOMER_NAME',
        render:(text,record)=>{
          return (
            <span>
              {`${record.firstName}${record.middleName}${record.lastName}`}
            </span>
          )
        }
      },
      {
        title: 'STATUS',
        dataIndex: 'status',
        key: 'status',
        render:(text,record)=>{
          let status_text
          transactionStatus.map((currentValue)=>{
            if (currentValue.key==record.status){
              status_text = currentValue.value
            }
          })
          return (
            <span>{status_text}</span>
          )
        }
      }
    ],
    pagination,
    location,
    onChange (page) {
      const { query, pathname } = location
      dispatch(routerRedux.push({
        pathname,
        query: {
          ...query,
          page: page.current,
          pageSize: page.pageSize,
        },
      }))
    },
    onDeleteItem (id) {
      dispatch({
        type: 'transactionReport/delete',
        payload: id,
      })
    },
    onEditItem (item) {
      dispatch(routerRedux.push({
        pathname: `/transactionreport/detail/:${item.id}`,
      }))
    },
    onReportItem(id){
      dispatch({
        type:'transactionReport/reported',
        payload:{
          id:id
        }
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'transactionReport/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
  }
  const filterProps = {
    filter: {
      ...location.query,
    },
    onFilterChange (value) {
      dispatch(routerRedux.push({
        pathname: location.pathname,
        query: {
          ...value,
          page: 1,
          pageSize,
        },
      }))
    },
    onSearch (fieldsValue) {
      fieldsValue.keyword.length ? dispatch(routerRedux.push({
        pathname: '/transactionReport',
        query: {
          field: fieldsValue.field,
          keyword: fieldsValue.keyword,
        },
      })) : dispatch(routerRedux.push({
        pathname: '/transactionReport',
      }))
    },
    onAdd () {
      dispatch(routerRedux.push({
        pathname: `/transactionReport/detail/${config.ADDFLAG}`,
      }))
    },
  }

  const handleDeleteItems = () => {
    dispatch({
      type: 'transactionReport/multiDelete',
      payload: {
        ids: selectedRowKeys,
      },
    })
  }

  return (
    <Page inner>
      <Filter {...filterProps} />
      {
        selectedRowKeys.length > 0 &&
        <Row style={{ marginBottom: 24, textAlign: 'right', fontSize: 13 }}>
          <Col>
            {`Selected ${selectedRowKeys.length} items `}
            <Popconfirm title={'Are you sure delete these items?'} placement="left" onConfirm={handleDeleteItems}>
              <Button type="primary" size="large" style={{ marginLeft: 8 }}>Remove</Button>
            </Popconfirm>
          </Col>
        </Row>
      }
      <List {...listProps} />
    </Page>
  )
}

transactionReport.propTypes = {
  transactionReport: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({ transactionReport, loading }) => ({ transactionReport, loading }))(transactionReport)
